import { ApiProperty } from "@nestjs/swagger";
import { User } from "src/user/entities/user.entity";
import { Column, CreateDateColumn, Entity, JoinColumn, ManyToOne, OneToMany, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";

@Entity('messageBoard')
export class MessageBoardEntity {
    @ApiProperty({ example: 'id', description: 'id' })
    @PrimaryGeneratedColumn({
        type: 'int',
        comment: 'id'
    })
    id: number;

    @ApiProperty({ example: 'content', description: '留言内容' })
    @Column({
        type: 'text',
        comment: '留言内容'
    })
    content: string;

    @ApiProperty({ example: 'createTime', description: '创建时间' })
    @CreateDateColumn({
        type: 'datetime',
        comment: '创建时间'
    })
    createTime: string

    @ApiProperty({ example: 'updateTime', description: '更新时间' })
    @UpdateDateColumn({
        type: 'datetime',
        comment: '更新时间'
    })
    updateTime: string

    @ApiProperty({ example: 'userId', description: '用户ID' })
    @Column({
        type: 'string',
        comment: '用户ID',
    })
    userId: string;
    @ManyToOne((type) => User, (user) => user.id)
    @JoinColumn({ name: 'userId' })
    user: User;

    @ApiProperty({ example: 'toUserId', description: '被回复用户ID' })
    @Column({
        type: 'string',
        comment: '被回复用户ID',
        nullable: true
    })
    toUserId: string;
    @ManyToOne(() => User, (user) => user.id)
    @JoinColumn({ name: 'toUserId' })
    toUser?: User;

    @ApiProperty({ example: 'messageBoardId', description: '留言ID' })
    @Column({
        type: 'int',
        comment: '留言ID',
        nullable: true

    })
    messageBoardId: number;


    @ApiProperty({ example: 'parseId', description: '父ID' })
    @Column({
        type: 'int',
        comment: '父ID',
        nullable: true

    })
    parseId: number;

    @ManyToOne(() => MessageBoardEntity, (messageBoard) => messageBoard.id)
    @JoinColumn({ name: 'messageBoardId' })
    messageBoard: MessageBoardEntity;

    @ApiProperty({ example: 'twoMessageBoard', description: '留言' })
    @OneToMany(() => MessageBoardEntity, (messageBoard) => messageBoard.messageBoard)
    twoMessageBoard?: MessageBoardEntity[]

    userInfo: {};

    toResponseObject(): MessageBoardEntity {
        let responseObj: MessageBoardEntity = {
            ...this
        };
        if (this.user && this.user.id) {
            responseObj.userInfo = {
                userId: this.user.id,
                author: this.user.nickname || this.user.username,
                avatar: this.user.avatar
            }
        }
        return responseObj;
    }
}
